/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.bancodedados.daoimplemente.mysql;

import camada.bancodedados.conexao.Conexao;
import eloma.classe.mysql.Campos;
import eloma.classe.mysql.tableaMysql;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author KLINGER
 */
public class MysqlDAOImpl {

    tableaMysql tabela = new tableaMysql();

    public void criarString() {

        String sql = "CREATE TABLE " + tabela.getTabela() + " (";

        for (Campos campo : tabela.getListaCampos()) {

           //`id`  int NOT NULL AUTO_INCREMENT ,
//           if(campo.isChaveUnica() == ) 
        }
    }

    public List<String> getTabelasBanco() throws Exception {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            final String SQL_PESQUISA_USUARIO = "SHOW TABLES";
            stmt = Conexao.getCon().prepareStatement(SQL_PESQUISA_USUARIO);
            rs = stmt.executeQuery();

            List<String> listaTabelas = new ArrayList<String>();

            while (rs.next()) {
                listaTabelas.add(rs.getString(1));
            }

            return listaTabelas;

        } catch (SQLException sqle) {
            throw new Exception(sqle);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
        }
    }

    public String getScriptTabela(String tabela) throws Exception {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String script = "";
        try {
            final String SQL_PESQUISA_SCRIPT = "SHOW CREATE TABLE " + tabela;
            stmt = Conexao.getCon().prepareStatement(SQL_PESQUISA_SCRIPT);
            rs = stmt.executeQuery();

            while (rs.next()) {
                script = rs.getString(2);
            }

            return script;

        } catch (SQLException sqle) {
            throw new Exception(sqle);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
        }
    }

    public List<Campos> getCamposTabela(String tabela) throws Exception {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            final String SQL_PESQUISA_COLUMNS = "SHOW COLUMNS FROM " + tabela;
            stmt = Conexao.getCon().prepareStatement(SQL_PESQUISA_COLUMNS);
            rs = stmt.executeQuery();
            return carregarMultiplosResultados(rs, tabela);
        } catch (SQLException sqle) {
            throw new Exception(sqle);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
        }
    }

    private List<Campos> carregarMultiplosResultados(ResultSet rs, String tabela) throws SQLException {
        List<Campos> resultList = new ArrayList<Campos>();
        while (rs.next()) {
            Campos dto = new Campos();
            carregarVO(dto, rs, tabela);
            resultList.add(dto);
        }
        return resultList;
    }

    private void carregarVO(Campos dto, ResultSet rs, String tabela) throws SQLException {
        dto.setTabela(tabela);
        dto.setField(rs.getString("Field"));
        dto.setType(rs.getString("Type"));
        dto.setNul(rs.getBoolean("Null"));
        dto.setKey(rs.getString("Key"));
        dto.setDefaul(rs.getBoolean("Default"));
        dto.setExtra(rs.getString("Extra"));
    }

}
